import { createRouter, createWebHistory } from 'vue-router'
import AppLayout from '../layouts/AppLayout.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('../pages/Login.vue'),
      meta: { public: true }
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('../pages/Register.vue'),
      meta: { public: true }
    },
    {
      path: '/',
      component: AppLayout,
      redirect: '/dashboard',
      meta: { requiresAuth: true },
      children: [
        {
          path: '/dashboard',
          name: 'Dashboard',
          component: () => import('../pages/Dashboard.vue'),
          meta: { title: '仪表盘' }
        },
        {
          path: '/hosts',
          name: 'HostManagement',
          component: () => import('../pages/HostManagement.vue'),
          meta: { title: '主机管理' }
        },
        {
          path: '/host-groups',
          name: 'HostGroupManagement',
          component: () => import('../pages/HostGroupManagement.vue'),
          meta: { title: '主机组管理' }
        },
        {
          path: '/projects',
          name: 'ProjectManagement',
          component: () => import('../pages/ProjectManagement.vue'),
          meta: { title: '项目管理' }
        },
        {
          path: '/environments',
          name: 'EnvironmentManagement',
          component: () => import('../pages/EnvironmentManagement.vue'),
          meta: { title: '环境管理' }
        },
        {
          path: '/users',
          name: 'UserManagement',
          component: () => import('../pages/UserManagement.vue'),
          meta: { title: '用户管理' }
        },
        {
          path: '/user-groups',
          name: 'UserGroupManagement',
          component: () => import('../pages/UserGroupManagement.vue'),
          meta: { title: '用户组管理' }
        },
        {
          path: '/roles',
          name: 'RoleManagement',
          component: () => import('../pages/RoleManagement.vue'),
          meta: { title: '角色管理' }
        },
        {
          path: '/email-config',
          name: 'EmailConfig',
          component: () => import('../pages/EmailConfig.vue'),
          meta: { title: '邮箱配置' }
        },
        {
          path: '/audit-log',
          name: 'AuditLog',
          component: () => import('../pages/AuditLog.vue'),
          meta: { title: '日志审计' }
        },
        // Ansible Management
        {
          path: '/ansible/playbooks',
          name: 'AnsiblePlaybooks',
          component: () => import('../pages/AnsiblePlaybooks.vue'),
          meta: { title: 'Playbook执行' }
        },
        {
          path: '/ansible/inventories',
          name: 'AnsibleInventories',
          component: () => import('../pages/AnsibleInventories.vue'),
          meta: { title: '配置管理' }
        },
        {
          path: '/ansible/credentials',
          name: 'AnsibleCredentials',
          component: () => import('../pages/AnsibleCredentials.vue'),
          meta: { title: '凭据管理' }
        },
        {
          path: '/ansible/adhoc',
          name: 'AnsibleAdhoc',
          component: () => import('../pages/AnsibleAdhoc.vue'),
          meta: { title: 'Ad-hoc执行' }
        },
        {
          path: '/ansible/jobs',
          name: 'AnsibleJobs',
          component: () => import('../pages/AnsibleJobs.vue'),
          meta: { title: '任务历史' }
        },
        {
          path: '/ssh-manager',
          name: 'SSHManager',
          component: () => import('../pages/SSHManager.vue'),
          meta: { title: 'SSH连接管理' }
        },
        // 加载组件演示
        {
          path: '/loading-demo',
          name: 'LoadingDemo',
          component: () => import('../pages/LoadingDemo.vue'),
          meta: { title: '加载组件演示' }
        },
        // 主机详情页面
        {
          path: '/host/:hostId',
          name: 'HostDetail',
          component: () => import('../pages/HostDetail.vue'),
          meta: { title: '主机详情' }
        },
        // 性能趋势分析
        {
          path: '/performance-trend',
          name: 'PerformanceTrend',
          component: () => import('../pages/PerformanceTrend.vue'),
          meta: { title: '性能趋势分析' }
        }
      ]
    }
  ]
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const isAuthenticated = !!token
  
  // 如果路由需要认证
  if (to.matched.some(record => record.meta.requiresAuth !== false && !record.meta.public)) {
    if (!isAuthenticated) {
      // 未登录，跳转到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else if (to.meta.public && isAuthenticated) {
    // 已登录用户访问登录/注册页，跳转到首页
    next('/dashboard')
  } else {
    next()
  }
})

export default router
